<?php

namespace App\Http\Controllers\Cruise\Feishu;

use App\Enums\Common\CommonEnum;
use App\Http\Controllers\BaseController;
use App\Models\Common\ResourcesModel;
use App\Models\Cruise\CruiseCheckRectifyModel;
use App\Models\Cruise\CruiseCheckRectifyReplyModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckCompanyService;
use App\Service\Cruise\Check\CruiseCheckRectifyService;
use App\Service\Cruise\Check\CruiseCheckSelfTaskService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CruiseCheckRectifyAppletController extends BaseController
{

    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function listsCount(Request $request): JsonResponse
    {
        $shopId   = (int)$request->header('shopId', 0);
        $templateType = (int)$request->get('templateType', CruiseTemplateModel::TYPE_3);
        $cruiseCheckCompanyIds = [];

        //判断用户的权限
        if ($templateType == CruiseOrderModel::TYPE_COMPANY_CHECK && !CruiseCheckSelfTaskService::hasSuperAuth()) {
            $cruiseCheckCompanyIds = CruiseCheckCompanyService::getPowerList(getRmsUserId());
        }

        return $this->success([
            'agg' => collect(CruiseCheckRectifyModel::STATUS_TEXT)->map(function ($text, $status) use ($shopId, $templateType, $cruiseCheckCompanyIds) {
                return [
                    'status' => $status,
                    'text'   => $text,
                    'count'  => CruiseCheckRectifyService::agg($shopId, $status, $templateType, $cruiseCheckCompanyIds),
                ];
            })->values()
        ]);
    }


    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function lists(Request $request): JsonResponse
    {

        $this->validate($request, [
            'status'   => 'integer',
        ]);

        $pageNum      = (int)$request->get('pageNum', 1);
        $pageSize     = (int)$request->get('pageSize', 10);
        $status       = (int)$request->get('status', CruiseCheckRectifyModel::STATUS_WAIT_TOTAL);
        $dealUid      = (int)$request->get('dealUid', 0);
        $shopId       = (int)$request->header('shopId');
        $templateType = (int)$request->get('templateType', CruiseTemplateModel::TYPE_5);
        $category     = (int)$request->header('category', 0);
        $request->merge(['shopId' => $shopId]);
        $cruiseCheckCompanyIds = [];

        //判断用户的权限
        if ($category == CruiseOrderModel::TYPE_COMPANY_CHECK && !CruiseCheckSelfTaskService::hasSuperAuth()) {
            $cruiseCheckCompanyIds = CruiseCheckCompanyService::getPowerList(getRmsUserId());
        }

        $currentUid = 0;

        return $this->success(
            (new CruiseCheckRectifyService(0, $currentUid))->lists(null, $status, $pageSize, $pageNum, 0, 0, $shopId, $templateType, $cruiseCheckCompanyIds, $dealUid)
        );
    }

    public function detail(Request $request, int $id): JsonResponse
    {
        $currentUid = getRmsUserId();

        return $this->success((new CruiseCheckRectifyService($id, $currentUid))->detail(CommonEnum::PLATFORM_FS_MINI));
    }

    public function submit(Request $request, int $id): JsonResponse
    {
        $files = $request->post('files');
        $desc  = $request->post('desc');

        $this->validate($request, [
            'files'        => 'required|array',
            'files.*.url'  => 'required|string',
            'files.*.type' => ['required', 'string', Rule::in(ResourcesModel::EXTRA_TYPES)],
            'desc'         => 'required|string',
        ]);

        $currentUid = getRmsUserId();

        (new CruiseCheckRectifyService($id, $currentUid))->submitReply($files, $desc);

        return $this->success();
    }

    public function replyDetail(Request $request, int $id)
    {
        return $this->success([
            'rectifyReply' => CruiseCheckRectifyReplyModel::getDataByIds([$id])[$id] ?? [],
        ]);
    }

}
